package org.jivesoftware.smackx;

import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.IQTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.packet.LastActivity;

public class LastActivityManager
{
  private Connection connection;
  private long lastMessageSent;

  static
  {
    Connection.addConnectionCreationListener(new ConnectionCreationListener()
    {
      public void connectionCreated()
      {
        new LastActivityManager(paramConnection, null);
      }
    });
  }

  private LastActivityManager(Connection paramConnection)
  {
    this.connection = paramConnection;
    paramConnection.addPacketSendingListener(new PacketListener(this)
    {
      public void processPacket()
      {
        LastActivityManager.access$1(this.this$0);
      }
    }
    , null);
    3 local3 = new PacketListener(this)
    {
      public void processPacket()
      {
        LastActivity localLastActivity = new LastActivity();
        localLastActivity.setType(IQ.Type.RESULT);
        localLastActivity.setTo(paramPacket.getFrom());
        localLastActivity.setFrom(paramPacket.getTo());
        localLastActivity.setPacketID(paramPacket.getPacketID());
        localLastActivity.setLastActivity(LastActivityManager.access$2(this.this$0));
        LastActivityManager.access$3(this.this$0).sendPacket(localLastActivity);
      }
    };
    PacketFilter[] arrayOfPacketFilter = new PacketFilter[2];
    arrayOfPacketFilter[0] = new IQTypeFilter(IQ.Type.GET);
    arrayOfPacketFilter[1] = new PacketTypeFilter(LastActivity.class);
    paramConnection.addPacketListener(local3, new AndFilter(arrayOfPacketFilter));
  }

  private long getIdleTime()
  {
    return ((System.currentTimeMillis() - this.lastMessageSent) / 1000L);
  }

  public static LastActivity getLastActivity(Connection paramConnection, String paramString)
    throws XMPPException
  {
    LastActivity localLastActivity1 = new LastActivity();
    localLastActivity1.setTo(paramString);
    PacketCollector localPacketCollector = paramConnection.createPacketCollector(new PacketIDFilter(localLastActivity1.getPacketID()));
    paramConnection.sendPacket(localLastActivity1);
    LastActivity localLastActivity2 = (LastActivity)localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    localPacketCollector.cancel();
    if (localLastActivity2 == null)
      throw new XMPPException("No response from server on status set.");
    if (localLastActivity2.getError() != null)
      throw new XMPPException(localLastActivity2.getError());
    return localLastActivity2;
  }

  private void resetIdleTime()
  {
    this.lastMessageSent = System.currentTimeMillis();
  }
}